package cn.rkylin.oms.accountreport.controller;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.DateUtil;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.system.shop.service.ShopServiceImpl;
import cn.rkylin.oms.system.shop.vo.ShopVO;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rkylin.oms.accountreport.adapter.AccountreportAdatper;
import cn.rkylin.oms.accountreport.adapter.AccountreportAdatperFactory;

@Controller
@RequestMapping("/accountreportManager")
public class AccountreportController {

    private static final Log logger = LogFactory.getLog(AccountreportController.class);
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    ShopServiceImpl shopService;

    @Autowired
    LogUtil logUtil;

    /**
     * http://localhost:8081/accountreportManager/getAccountreportAll.action
     * start, end 依然需要正常传递, 否则无法获取7天的数据
     */
    @ResponseBody
    @RequestMapping(value = "/getAccountreportAll", method = RequestMethod.GET)
    public Map getAccountreportAll(String shopId, String start, String end) {
        // 返回对象
        Map<String, Object> resultMap = new HashMap<String, Object>();

        try {
//            shopId = "008480ccb1d64b30b843023fe5b13d3a";
//            end = "2017-06-22 23:59:59";

            if (StringUtils.isEmpty(start) && StringUtils.isNotEmpty(end)) {
                Calendar c = Calendar.getInstance();
                c.setTime(simpleDateFormat.parse(end));
                // 得到前7天
                c.add(Calendar.DATE, -7);
                start = simpleDateFormat.format(c.getTime());
            }

            if (StringUtils.isBlank(start) && StringUtils.isBlank(end)) {
                ShopVO shopVO = shopService.findById(shopId);
                if (shopVO.getJobEcAccountLastUpdateTime() == null) {
                    start = simpleDateFormat.format(new Date());
                } else {
                    start = simpleDateFormat.format(shopVO.getJobEcAccountLastUpdateTime());
                }
                end = DateUtil.getCurrentDateStr("yyyy-MM-dd HH:mm:ss");
            }

            start = URLDecoder.decode(start, "UTF-8");
            end = URLDecoder.decode(end, "UTF-8");
            logger.debug("【jobExec.start】" + start);
            logger.debug("【jobExec.end】" + end);

            AccountreportAdatper adapter = AccountreportAdatperFactory.getItemAdapter(shopId);
            if (adapter == null) {
                resultMap.put("success", "false");
                resultMap.put("msg", "adapter is null");
                return resultMap;
            }

            // 请求参数设置
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startTime", start);
            paramMap.put("endTime", end);

            int r = adapter.download(shopId, paramMap);
            if (r == 0) {
                resultMap.put("success", "false");
            } else {
                ShopVO shopUpdate = new ShopVO();
                shopUpdate.setShopId(shopId);
                shopUpdate.setJobEcAccountLastUpdateTime((simpleDateFormat.parse(end)));
                shopService.update(shopUpdate);
                resultMap.put("success", "true");
            }
        } catch (Exception e) {
            resultMap.put("success", "false");
            resultMap.put("msg", e.getMessage());
            logUtil.error(new DBLog("", "job_exception", "getAccountreportAll",
                    e.getMessage()), e);
        }
        return resultMap;
    }
}
